﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServiceSchool
{
    public class ServiceSchool : IServiceSchool
    {
        public List<SubjectDTO> GetSubjects(string className)
        {
            SchoolEntities context = new SchoolEntities();
            Class classFound = context.Classes.Where(x => x.ClassName == className).Single();
            IEnumerable<ClassesSubject> connections = context.ClassesSubjects.Where(x => x.ClassId == classFound.ClassId);
            List<SubjectDTO> subjects = new List<SubjectDTO>();
            foreach (ClassesSubject connection in connections)
            {
                Subject newSubject = context.Subjects.Where(x => x.SubjectId == connection.SubjectId).First();
                subjects.Add(SubjectDTO.CreateFromSubject(newSubject));
            }
            return subjects;
        }

        public List<StudentDTO> GetStudents(string className)
        {
            SchoolEntities context = new SchoolEntities();
            Class classFound = context.Classes.Where(x => x.ClassName == className).Single();
            IEnumerable<Student> students = context.Students.Where(x => x.ClassId == classFound.ClassId);
            List<StudentDTO> studentDTOs = new List<StudentDTO>();
            foreach (Student student in students)
            {
                studentDTOs.Add(StudentDTO.CreateFromStudent(student));
            }
            return studentDTOs;
        }

        public string AddClass(string className)
        {
            SchoolEntities context = new SchoolEntities();
            Class newClass = new Class();
            newClass.ClassName = className;
            context.Classes.AddObject(newClass);
            context.SaveChanges();
            string result = String.Format("Class with name {0} was added with id={1}!", className, newClass.ClassId);
            return result;
        }

        public string DeleteClass(string className)
        {
            SchoolEntities context = new SchoolEntities();
            Class classToDelete = context.Classes.Where(x => x.ClassName == className).First();
            context.Classes.DeleteObject(classToDelete);
            context.SaveChanges();
            string result = String.Format("Class with name {0} was deleted!", className);
            return result;
        }

        public string AddStudent(string studentName)
        {
            SchoolEntities context = new SchoolEntities();
            Student newStudent = new Student();
            newStudent.Name = studentName;
            context.Students.AddObject(newStudent);
            context.SaveChanges();
            string result = String.Format("Student with name {0} was added with id={1}!", 
                studentName, newStudent.StudentId);
            return result;
        }

        public string EditStudent(string studentName, string newName)
        {
            SchoolEntities context = new SchoolEntities();
            Student studentToEdit = context.Students.Where(x => x.Name == studentName).First();
            studentToEdit.Name = newName;
            context.SaveChanges();
            string result = String.Format("Student with name {0} was renamed to {1}!", studentName, newName);
            return result;
        }

        public string DeleteStudent(string studentName)
        {
            SchoolEntities context = new SchoolEntities();
            Student studentToDelete = context.Students.Where(x => x.Name == studentName).First();
            context.Students.DeleteObject(studentToDelete);
            context.SaveChanges();
            string result = String.Format("Student with name {0} was deleted!", studentName);
            return result;
        }

        public string AddStudentToClass(string studentName, string className)
        {
            SchoolEntities context = new SchoolEntities();
            Student studentToAdd = context.Students.Where(x => x.Name == studentName).First();
            Class classToAdd = context.Classes.Where(x => x.ClassName == className).First();
            studentToAdd.ClassId = classToAdd.ClassId;
            context.SaveChanges();
            string result = String.Format("Student with name {0} was added to class {1}!", studentName, className);
            return result;
        }

        public string RemoveStudentFromClass(string studentName)
        {
            SchoolEntities context = new SchoolEntities();
            Student studentToEdit = context.Students.Where(x => x.Name == studentName).First();
            Class studentClass = context.Classes.Where(x => x.ClassId == studentToEdit.ClassId).First();
            studentToEdit.ClassId = null;
            context.SaveChanges();
            string result = String.Format("Student with name {0} was removed from class {1}!", 
                studentName, studentClass.ClassName);
            return result;
        }

        public string AddSubject(string subjectName)
        {
            SchoolEntities context = new SchoolEntities();
            Subject newSubject = new Subject();
            newSubject.SubjectName = subjectName;
            context.Subjects.AddObject(newSubject);
            context.SaveChanges();
            string result = String.Format("Subject with name {0} was added with id={1}!", 
                subjectName, newSubject.SubjectId);
            return result;
        }

        public string EditSubject(string subjectName, string newName)
        {
            SchoolEntities context = new SchoolEntities();
            Subject subjectToEdit = context.Subjects.Where(x => x.SubjectName == subjectName).First();
            subjectToEdit.SubjectName = newName;
            context.SaveChanges();
            string result = String.Format("Subject with name {0} was renamed to {1}!", subjectName, newName);
            return result;
        }

        public string DeleteSubject(string subjectName)
        {
            SchoolEntities context = new SchoolEntities();
            Subject subjectToDelete = context.Subjects.Where(x => x.SubjectName == subjectName).First();
            context.Subjects.DeleteObject(subjectToDelete);
            context.SaveChanges();
            string result = String.Format("Subject with name {0} was deleted!", subjectName);
            return result;
        }

        public string AddSubjectToClass(string subjectName, string className)
        {
            SchoolEntities context = new SchoolEntities();
            Subject subjectToAdd = context.Subjects.Where(x => x.SubjectName == subjectName).First();
            Class classToAdd = context.Classes.Where(x => x.ClassName == className).First();
            ClassesSubject newConnection = new ClassesSubject();
            newConnection.SubjectId = subjectToAdd.SubjectId;
            newConnection.ClassId = classToAdd.ClassId;
            context.ClassesSubjects.AddObject(newConnection);
            context.SaveChanges();
            string result = String.Format("Subject with name {0} was added to class {1}!", subjectName, className);
            return result;
        }

        public string RemoveSubjectFromClass(string subjectName, string className)
        {
            SchoolEntities context = new SchoolEntities();
            Subject subjectToRemove = context.Subjects.Where(x => x.SubjectName == subjectName).First();
            Class classToRemove = context.Classes.Where(x => x.ClassName == className).First();
            ClassesSubject connectionToDelete = context.ClassesSubjects.Where(
                x => x.SubjectId == subjectToRemove.SubjectId && x.ClassId == classToRemove.ClassId).First();
            context.ClassesSubjects.DeleteObject(connectionToDelete);
            context.SaveChanges();
            string result = String.Format("Subject with name {0} was removed from class {1}!", subjectName, className);
            return result;
        }

        public List<SubjectDTO> GetAllSubjects()
        {
            SchoolEntities context = new SchoolEntities();
            IEnumerable<Subject> subjects = context.Subjects;
            List<SubjectDTO> list = new List<SubjectDTO>();
            foreach (Subject subject in subjects)
            {
                list.Add(SubjectDTO.CreateFromSubject(subject));
            }
            return list;
        }
    }
}
